package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //新增，需要同时判断权限和菜单
    @Override
    public void add(Role role, Integer[] permissionIds,Integer[] menuIds) {
        roleDao.add(role);
        if(permissionIds != null && permissionIds.length>0 ) {
            setRoleAndPermission(role.getId(), permissionIds);
        }
        if(menuIds != null && menuIds.length>0){
            setRoleAndMenu(role.getId(), menuIds);
        }
    }


    //分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page=roleDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }



    //向中间表角色和权限表中插入数据
    private void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if(permissionIds != null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("permission_id",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }


    //向中间表菜单和角色表中
    private void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if(menuIds != null && menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("role_id",roleId);
                map.put("menu_id",menuId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }


    //根据id查询权限信息
    public List<Integer> findPermissonByRoleId(Integer id) {
        return roleDao.findPermissonByRoleId(id);

    }

    //根据id查询菜单信息
    public List<Integer> findMenuByRoleId(Integer id) {
        return roleDao.findMenuByRoleId(id);

    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }


    public void edit(Role role, Integer[] permissionIds,Integer[] menuIds) {
        //根据检查组id删除关联表
        roleDao.deleteAssociationpermission(role.getId());
        roleDao.deleteAssociationmenu(role.getId());


        //向中间表中插入新连接关系
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);

        //更新检查组信息
        roleDao.edit(role);
    }

    @Override
    public void delete(Integer id) throws RuntimeException{
        long count =  roleDao.findPermissionCountByRoleId(id);
        long count1 = roleDao.findMenuCountByRoleId(id);
        if(count >0 || count1 > 0){
            throw new RuntimeException("当前角色被引用，不能删除");
        }
        roleDao.deleteById(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

//
//    public List<Map<String, Object>> findRoleCount() {
//
//        //查询套餐数据
//        return roleDao.findRoleCount();
//    }
}
